package com.zhangxu.microservice.system.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.system.dao.entity.RoleResourceDO;
import com.zhangxu.microservice.system.dao.mapper.RoleResourceMapper;
import com.zhangxu.microservice.system.dao.service.RoleResourceDao;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色资源权限关联DAO实现类
 */
@Service
public class RoleResourceDaoImpl extends ServiceImpl<RoleResourceMapper, RoleResourceDO> implements RoleResourceDao {

    @Override
    public List<RoleResourceDO> listByRoleIdAndAppId(Long roleId, Long appId) {
        return this.lambdaQuery()
                .eq(RoleResourceDO::getRoleId, roleId)
                .eq(RoleResourceDO::getAppId, appId)
                .eq(RoleResourceDO::getDeleteTime, 0L)
                .list();
    }

    @Override
    public List<RoleResourceDO> listByRoleId(Long roleId) {
        return this.lambdaQuery()
                .eq(RoleResourceDO::getRoleId, roleId)
                .eq(RoleResourceDO::getDeleteTime, 0L)
                .list();
    }

    @Override
    public List<RoleResourceDO> listByResourceCode(String resourceCode) {
        LambdaQueryWrapper<RoleResourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResourceDO::getResourceCode, resourceCode)
                .eq(RoleResourceDO::getDeleteTime, 0L);
        return list(queryWrapper);
    }

    @Override
    public RoleResourceDO getByRoleIdAndResourceCode(Long roleId, String resourceCode) {
        LambdaQueryWrapper<RoleResourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResourceDO::getRoleId, roleId)
                .eq(RoleResourceDO::getResourceCode, resourceCode)
                .eq(RoleResourceDO::getDeleteTime, 0L);
        return getOne(queryWrapper);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<RoleResourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResourceDO::getRoleId, roleId)
                .eq(RoleResourceDO::getDeleteTime, 0L);

        List<RoleResourceDO> roleResourceList = list(queryWrapper);
        if (!roleResourceList.isEmpty()) {
            Long currentTime = System.currentTimeMillis();
            roleResourceList.forEach(roleResource -> roleResource.setDeleteTime(currentTime));
            updateBatchById(roleResourceList);
        }
    }

    @Override
    public void deleteByResourceCode(String resourceCode) {
        LambdaQueryWrapper<RoleResourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResourceDO::getResourceCode, resourceCode)
                .eq(RoleResourceDO::getDeleteTime, 0L);

        List<RoleResourceDO> roleResourceList = list(queryWrapper);
        if (!roleResourceList.isEmpty()) {
            Long currentTime = System.currentTimeMillis();
            roleResourceList.forEach(roleResource -> roleResource.setDeleteTime(currentTime));
            updateBatchById(roleResourceList);
        }
    }
}